package Modellus.WinUI.ExampleTable;

import Modellus.Core.Animator.IPropertyProxy;
import Modellus.Core.PersistableHelper;
import Modellus.WinUI.FramePersister;
import Modellus.WinUI.ExampleTable.ITableInternalFrame;
import Modellus.WinUI.ExampleTable.TableUI;
import org.w3c.dom.Node;

import java.util.Vector;

public class TablePersister extends FramePersister
{
	PersistableHelper _persistableHelper = new PersistableHelper();
	private ITableInternalFrame _tableInternalFrame;

	public TablePersister(ITableInternalFrame tableInternalFrame, String type)
	{
		super(tableInternalFrame, type);
		_tableInternalFrame = tableInternalFrame;
	}

	public void save(Node parentNode)
	{
		super.save(parentNode);
		TableUI tableUI = _tableInternalFrame.getTableUI();
		_tableInternalFrame.setChanged(false);
		_persistableHelper.saveBoolean(_headerNode, "ShowBars", tableUI.getShowBars());
		_persistableHelper.saveInteger(_headerNode, "Steps", tableUI.getSteps());
		Node termsNode = _persistableHelper.saveHeader(_headerNode, "Terms");
		Vector<IPropertyProxy> terms = tableUI.getVariables();
		for (int variableIndex = 0; variableIndex < terms.size(); variableIndex++)
		{
			_persistableHelper.saveProperty(termsNode, "Variable" + (variableIndex + 1), terms.get(variableIndex));
			_persistableHelper.saveColor(termsNode, "Color" + (variableIndex + 1), tableUI.getVariableColor(variableIndex));
		}
	}

	public void load(Node parentNode)
	{
		super.load(parentNode);
		if (_headerNode == null)
			return;
		TableUI tableUI = _tableInternalFrame.getTableUI();
		tableUI.setShowBars(_persistableHelper.loadBoolean(_headerNode, "ShowBars"));
		tableUI.setSteps(_persistableHelper.loadInteger(_headerNode, "Steps", 1));
		Node termsNode = _persistableHelper.loadHeader(_headerNode, "Terms");
		if (termsNode == null)
			return;
		Vector<IPropertyProxy> terms = tableUI.getVariables();
		for (int variableIndex = 0; variableIndex < terms.size(); variableIndex++)
		{
			_persistableHelper.loadProperty(termsNode, "Variable" + (variableIndex + 1), terms.get(variableIndex));
			tableUI.reset(variableIndex);
		}
		for (int variableIndex = 0; variableIndex < terms.size(); variableIndex++)
			tableUI.setVariableColor(variableIndex, _persistableHelper.loadColor(termsNode, "Color" + (variableIndex + 1)));
		tableUI.onShowBarsChanged();
		tableUI.onVariablesChanged();
		tableUI.onStepsChanged();
	}
}